package kr.hkit.powerjava.chapter26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

import kr.hkit.powerjava.chapter26.common.DbUtil;
import kr.hkit.powerjava.chapter26.dao.DaoInterface;

public abstract class AbsService<T> {

	protected DaoInterface<T> dao;
	protected Scanner sc;
	protected Connection connection;

	public AbsService(Scanner sc, Connection connection) {
		this.sc = sc;
		this.connection = connection;
		//prnMenu();
	}

	private void close() {
		DbUtil.close(connection);
	}

	public abstract T inputItemInfo(boolean gubun);

	public abstract void prnSubMenu();

	public void prnMenu() {
		int res = 0;
		T item = null;

		do {
			System.out.print("1. 입력\n2. 삭제\n3. 수정\n4. 검색\n5. 리스트보기\n6. 종료\n");
			res = sc.nextInt();
			try {
				switch (res) {

				case 1:// 학생정보입력
					item = inputItemInfo(true);
					dao.insertItem(item);
					break;

				case 2:
					System.out.print("삭제할 정보를 입력하세요: ");
					item = inputItemInfo(false);
					dao.deleteItembyNo(item);
					break;

				case 3:
					item = inputItemInfo(true);
					dao.updateItembyNo(item);
					break;

				case 4:
					item = inputItemInfo(false);
					try {
						item = dao.searchItem(item);
						System.out.println(item);
					} catch (ArrayIndexOutOfBoundsException e) {
						System.out.println("해당 정보 없음");
					}
					break;

				case 5:
					prnSubMenu();
					break;

				default:
					res = 7;
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();

			}
		} while (res <= 6);
	}

}
